﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using TjZheng.Framework;
using TjZheng.Controls;
using TjZheng.Domain.Business;
using TjZheng.Domain.DataEntity;

namespace TjZheng.UpLoad
{
    public static partial class UpLoadifyExtension
    {
        /// <summary>
        /// 上传控件 jquery.uploadify.js
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="objUpLoadSet">上传配置</param>
        /// <returns></returns>
        public static MvcHtmlString UpLoadify(this HtmlHelper htmlHelper, UpLoadSet objUpLoadSet)
        {
            BizUploadLimit objBizUploadLimit = new BizUploadLimit();
            UploadLimit objUploadLimit = null;
            if (objUpLoadSet.Name.IsNull())
            {
                return MvcHtmlString.Create("<!--提交到服务器的Name不能为空-->");
            }
            if (objUpLoadSet.ULId > 0)
            {
                objUploadLimit = objBizUploadLimit.GetRecord(objUpLoadSet.ULId);
            }
            else
            {
                if (objUpLoadSet.CategoryCode.IsNoNull() && objUpLoadSet.LimitCode.IsNoNull())
                {
                    objUploadLimit = objBizUploadLimit.GetRecord(string.Format("UcCode='{0}' AND LimitCode='{1}'", objUpLoadSet.CategoryCode, objUpLoadSet.LimitCode));
                }
            }
            if (objUploadLimit == null)
            {
                return MvcHtmlString.Create("<!--未设置上传文件配置-->");
            }
            if (objUploadLimit.UcCode.IsNull() || objUploadLimit.LimitCode.IsNull())
            {
                return MvcHtmlString.Create("<!--上传文件的资源码和限制码不能为空-->");
            }
            string selector = objUpLoadSet.Name.Replace(".", "_");
            StringBuilder objStringBuilder = new StringBuilder();
            objStringBuilder.AppendLine(string.Format("<input id=\"{0}\" data-options='{{\"ULId\":\"{1}\",\"CategoryCode\":\"{2}\",\"LimitCode\":\"{3}\",Name:\"{4}\",\"ServerPath\":\"{5}\"}}' type=\"file\" />", selector, objUploadLimit.Id, objUploadLimit.UcCode, objUploadLimit.LimitCode, objUpLoadSet.Name, objUploadLimit.ServerPath));
            objStringBuilder.AppendLine(string.Format("<input name=\"{0}\" type=\"hidden\" value=\"{1}\" />", objUpLoadSet.Name, objUpLoadSet.Value));
            objStringBuilder.AppendLine("<script type=\"text/javascript\">");
            string tempName = string.Format("objFileUpload_{0}", Guid.NewGuid().ToString().Replace("-", ""));
            objStringBuilder.AppendLine(string.Format("var {0} = new FileUpload(\"{1}\");", tempName, selector.FormatSelf("#{0}")));
            if (objUpLoadSet.IsMulti)
            {
                objStringBuilder.AppendLine(string.Format("{0}.multi = true;", tempName));
            }
            objStringBuilder.AppendLine(string.Format("{0}.url = \"{1}\";", tempName, objUpLoadSet.Url));
            objStringBuilder.AppendLine(string.Format("{0}.formData = {{ \"UpLoadSet\": $('{1}').attr(\"data-options\") }};", tempName, selector.FormatSelf("#{0}")));
            objStringBuilder.AppendLine(string.Format("{0}.fileObjName = \"{1}\";", tempName, objUpLoadSet.FileName));
            objStringBuilder.AppendLine(string.Format("{0}.fileTypeExts = '{1}';", tempName, objUploadLimit.LimitExtension));
            if (objUploadLimit.LimitSize > 0)
            {
                objStringBuilder.AppendLine(string.Format("{0}.fileSizeLimit = {1};", tempName, objUploadLimit.LimitSize));
            }
            objStringBuilder.AppendLine(string.Format("{0}.uploadSuccess = {1};", tempName, objUpLoadSet.UpLoadSuccess));
            if (!string.IsNullOrEmpty(objUpLoadSet.ItemTemplate))
            {
                objStringBuilder.AppendLine(string.Format("{0}.itemTemplate = \"{1}\";", tempName, objUpLoadSet.ItemTemplate));
            }
            //延迟加载，防止谷歌崩溃
            objStringBuilder.AppendLine(string.Format("$(function () {{ setTimeout(function () {{{0}.uploadify(); }}, 10);}});", tempName));
            objStringBuilder.AppendLine("</script>");
            return MvcHtmlString.Create(objStringBuilder.ToString());
        }
    }
}